package com.ruoyi.contract.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.contract.domain.ContractInfo;
import com.ruoyi.contract.service.IContractInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 合同信息Controller
 *
 * @author ruoyi
 * @date 2022-08-31
 */
@RestController
@RequestMapping("/contract/ContractInfo")
public class ContractInfoController extends BaseController {
    @Autowired
    private IContractInfoService contractInfoService;

    /**
     * 查询合同信息列表
     */
    @PreAuthorize("@ss.hasPermi('contract:ContractInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ContractInfo contractInfo) {
        startPage();
        List<ContractInfo> list = contractInfoService.selectContractInfoList(contractInfo);
        return getDataTable(list);
    }

    /**
     * 导出合同信息列表
     */
    @PreAuthorize("@ss.hasPermi('contract:ContractInfo:export')")
    @Log(title = "合同信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ContractInfo contractInfo) {
        List<ContractInfo> list = contractInfoService.selectContractInfoList(contractInfo);
        ExcelUtil<ContractInfo> util = new ExcelUtil<ContractInfo>(ContractInfo.class);
        util.exportExcel(response, list, "合同信息数据");
    }

    /**
     * 获取合同信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('contract:ContractInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(contractInfoService.selectContractInfoById(id));
    }

    /**
     * 新增合同信息
     */
    @PreAuthorize("@ss.hasPermi('contract:ContractInfo:add')")
    @Log(title = "合同信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ContractInfo contractInfo) {
        if (StringUtils.isBlank(contractInfo.getOrderType())){
            throw new ServiceException("订单类型不能为空!");
        }
        if (StringUtils.isBlank(contractInfo.getContractName())){
            throw new ServiceException("合同名称不能为空!");
        }
        if (contractInfo.getStructCount() == null){
            throw new ServiceException("结构量不能为空!");
        }
        if (StringUtils.isBlank(contractInfo.getOppositeCompany())){
            throw new ServiceException("对方单位不能为空!");
        }
        if (StringUtils.isBlank(contractInfo.getContractAddress())){
            throw new ServiceException("合同地址不能为空!");
        }
        return toAjax(contractInfoService.insertContractInfo(contractInfo));
    }

    /**
     * 修改合同信息
     */
    @PreAuthorize("@ss.hasPermi('contract:ContractInfo:edit')")
    @Log(title = "合同信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ContractInfo contractInfo) {
        if (StringUtils.isBlank(contractInfo.getOrderType())){
            throw new ServiceException("订单类型不能为空!");
        }
        if (StringUtils.isBlank(contractInfo.getContractName())){
            throw new ServiceException("合同名称不能为空!");
        }
        if (contractInfo.getStructCount() == null){
            throw new ServiceException("结构量不能为空!");
        }
        if (StringUtils.isBlank(contractInfo.getOppositeCompany())){
            throw new ServiceException("对方单位不能为空!");
        }
        if (StringUtils.isBlank(contractInfo.getContractAddress())){
            throw new ServiceException("合同地址不能为空!");
        }
        return toAjax(contractInfoService.updateContractInfo(contractInfo));
    }

    /**
     * 删除合同信息
     */
    @PreAuthorize("@ss.hasPermi('contract:ContractInfo:remove')")
    @Log(title = "合同信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(contractInfoService.deleteContractInfoByIds(ids));
    }
}
